package com.guanghua.brick.html;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.guanghua.brick.security.SecurityUtil;
import com.guanghua.brick.util.IConfig;
import com.guanghua.brick.util.XmlReader;

public class TreeConfigContent implements IContent {
	
	private static Log logger = LogFactory.getLog(TreeConfigContent.class);
		
	private String id = null;
	private String file = null;
	private String primaryKeyProperty = "primaryKey";
	private String parentKeyProperty = "parentKey";
	private String textProperty = "text";
	private boolean permissionAvail = false;
	
	private int index = 0;
	
	public List<Map<String, String>> buildContent(HttpServletRequest request, HttpServletResponse response) {
		//读取xml
		try {
			XmlReader reader = new XmlReader(file, true);
			IConfig root = reader.readByTagName("content")[0];
			this.index = 0;
			
			List<Map<String, String>> list = parseTreeChildrenNode(request, root, -1);
			return list;
			
		} catch (Exception e) {
			logger.error("parse xml file to init xml config content error:" + file, e);
			return null;
		}
		
	}
	
	
	/**
	 * 递归够找配置树
	 */
	private List<Map<String, String>> parseTreeChildrenNode(HttpServletRequest request, IConfig parent, int parentKey) {
		IConfig[] nodes = parent.getChildren("node");
		//有子节点
		if (nodes != null) {
			List<Map<String, String>> list = new ArrayList<Map<String, String>>();
			for (int i = 0; i < nodes.length; i++) {
				
				//主键
				int primaryKey = this.index++;
				//功能点标志
				String function = nodes[i].getAttribute("function");
				//如果验证通过可以显示该节点
				if (function == null || !permissionAvail || SecurityUtil.functionPermissionValid(function, request)) {
					
					//获取子节点
					List<Map<String, String>> child = parseTreeChildrenNode(request, nodes[i], primaryKey);
					//是叶子节点或者子节点有不为空，那么该节点显示
					if (child == null || (child != null && !child.isEmpty())) {
						
						//构造节点
						Map<String, String> map = new HashMap<String, String>();
						map.put(this.primaryKeyProperty,  String.valueOf(primaryKey));
						map.put(this.parentKeyProperty, (parentKey==-1)?"":String.valueOf(parentKey));
						map.putAll(nodes[i].getAttributeMap());
						
						String text = nodes[i].getText();
						map.put(this.textProperty, (text==null)?"":text); 
						
						list.add(map);
						if (child != null) list.addAll(child);
					}
				}
			}
			return list;
		//无字节点
		} else {
			return null;
		}
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}


	public String getFile() {
		return file;
	}


	public void setFile(String file) {
		this.file = file;
	}


	public String getParentKeyProperty() {
		return parentKeyProperty;
	}


	public void setParentKeyProperty(String parentKeyProperty) {
		this.parentKeyProperty = parentKeyProperty;
	}


	public boolean isPermissionAvail() {
		return permissionAvail;
	}


	public void setPermissionAvail(boolean permissionAvail) {
		this.permissionAvail = permissionAvail;
	}


	public String getPrimaryKeyProperty() {
		return primaryKeyProperty;
	}


	public void setPrimaryKeyProperty(String primaryKeyProperty) {
		this.primaryKeyProperty = primaryKeyProperty;
	}


	public String getTextProperty() {
		return textProperty;
	}


	public void setTextProperty(String textProperty) {
		this.textProperty = textProperty;
	}

}
